package com.kyuketsukistudios.mikatsuki.data 
{
	import com.carlcalderon.arthropod.Debug;
	import flash.events.Event;
	import flash.filesystem.File;
	import flash.filesystem.FileMode;
	import flash.filesystem.FileStream;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	/**
	 * ...
	 * @author RZEROSTERN
	 * 
	 * Class SINGLETON for Data Management.
	 * It's very recommended if we are going to upload Mikatsuki at Facebook, Kongregate, Newgrounds
	 * or any sites like that. Also is recommended for database connections. Ehm...this fucking
	 * teacher is talking bullshit again (@Memories and Peripherals class, UNITECMX).
	 * 
	 */
	public class SingletonData 
	{
		private static var instance:SingletonData; //This is my instance
		private static var m_allowInstance:Boolean; //This flag will handle if we can create an instance or not.
		private static var m_fileStream:FileStream;
		
		/*Public functions*/
		public function SingletonData() //Main constructor for our class
		{
			if (!m_allowInstance) //If flag is false, this shit will not create anything.
			{
				throw new Error("This is a singleton class."); //Throws error ¬¬'
			}
			else
			{
				Debug.log("New Singleton instance created.", 0x00ff00); //Arthropod says, TOUCHE \m/ !!!
			}
		}
		
		public static function getInstance():SingletonData //We need to get an instance.
		{
			if (instance == null) //If we don't have an instance...
			{
				m_allowInstance = true; //Allow the creation of our instance.
				instance = new SingletonData(); //VOILÀ, NEW INSTANCE !!!
				m_allowInstance = false; //Shut the door for creating instances !!!
			}
			else // When we have already created the instance, we'll use that for hard work...
			{
				
			}
			return instance; //Return instance to game.
		}
		
		/*A beautiful function for calling Arthropod. We'll use this for sending console data.*/
		public function sendToConsole(p_color:uint, ... args):void 
		{
			Debug.log(args, p_color);
		}
		
		/* AWESOME SAVE FILE
		 * Courtesy of daddy STERN. 
		 * writeSaveGame.- Will save the game. We should define the string or params to save.
		 * loadSaveFile.- Will read the file. After that it'll split it into an array and return it
		 */
		
		public function writeSaveFile(p_stringReceived:String):void
		{
			/* 
			 * We need to specify the program where is the root of our file system 
			 * - C:\Program Files\<your AIR app name> for x86 systems.
			 * - C:\Program Files (x86)\<your AIR app name> for x64 systems.
			 */
			var applicationDirectoryPath:File = File.applicationDirectory;
			//Cast that path into an string...
			var nativePathToApplicationDirectory:String = applicationDirectoryPath.nativePath.toString(); 
			//...and join it with the name of your file.
			nativePathToApplicationDirectory += "/save/save.mik";
			//Call a new instance of File with reference to the path we have created.
			var file:File = new File(nativePathToApplicationDirectory);
			
			var filestream:FileStream = new FileStream(); //New instance of Filestream.
			filestream.open(file, FileMode.UPDATE); //OPEN SESAMUS.
			//Write stuff on the file. You can handle any kind of file, or even create your own file kinds.
			filestream.writeUTFBytes(p_stringReceived); 
			filestream.close(); // Close the file.
		}
		
		/* LOVELY
		 * I want to load my MIK file. OK, here we go.
		 * You can view an explaination on the previous function, it's almost the same shit.*/
		public function loadSaveFile():Array
		{
			var applicationDirectoryPath:File = File.applicationDirectory;
			var nativePathToApplicationDirectory:String = applicationDirectoryPath.nativePath.toString();
			nativePathToApplicationDirectory+= "/save/save.mik";
			var file:File = new File(nativePathToApplicationDirectory);
			
			var filestream:FileStream = new FileStream();
			filestream.open(file, FileMode.READ); //OPEN in Read Only mode.
			var str:String = filestream.readUTFBytes(filestream.bytesAvailable); //Cast our content into an string.
			var array:Array = str.split(/\n/); //Split our string getting the RETURN UTF key ("\n").
			filestream.close();// Close the file.
			
			return array;
		}
		
	}

}